<?php
/**
 * This file contains the class PersonNoticesModel which provides
 * most of the code requirements for the Member page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
/**
 * This class provides the code for the person notices page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
class PersonNoticesModel
{
    /**
     * Access to the database methods
     *
     * @var MemberDB
     */
    private $_db;
    /**
     * Access to the View methods
     *
     * @var MemberView
     */
    private $_view;
    /**
     * Access to the Person methods
     *
     * @var PersonModel
     */
    private $_personModel;
    /**
     * Create a new instance of this class
     *
     * @return void
     * @param MemberDB $db
     * @param MemberView $view
     * @param PersonModel $personModel
     */
    public function __construct($db, $view, $personModel)
    {
        $this->_db = $db;
        $this->_view = $view;
        $this->_personModel = $personModel;
    }
    /**
     * Show the person info
     *
     * @return string
     * @param array[string]mixed $person
     * @param int $publishedNotices
     */
     function personInfo($person, $publishedNotices)
    {
        $view = $this->_view;
        $personModel = $this->_personModel;
        $personId = $person['id'];
        $firstNames = $person['firstNames'];
        $surname = strtoupper($person['surname']);
        $akaName = $personModel->showaka($person['alsoKnownAs']);
        $neeAndFormerName = $personModel->showneeandformer($person['neeName'], $person['formerName']);
        $formattedDate = $personModel->formatdatetime($person['deathDate']);
        $deathDate = $personModel->showdeathdate($formattedDate);
        $age = $personModel->showage($person['age'],$person['ageMeasure']);
        $country = $person['country'];
        $personInfo = array(
            'header' => $view->personHeader($surname, $firstNames),
            'details' => $view->personInfo($personId, $surname, $firstNames, $akaName, $neeAndFormerName, $deathDate, $age, $publishedNotices, $country)
        );
        return $personInfo;
    }
    function getNewspaperName($publicationName)
    {
        $db = $this->_db;
        $nameParts = explode("-",$publicationName);
        return $db->getNewspaperName($nameParts[2]);
    }
    function noticeListings($personId, $publications)
    {
        $db = $this->_db;
        $view = $this->_view;
        $personModel = $this->_personModel;
        $noticeListings = '';
        foreach ($publications as $publication) {
            $newspaper = $this->getNewspaperName($publication['name']);
            $url = 'deathNoticeAdvice.php?personid='.$personId.'&publication='.$publication['name'];
            $datePublished = $personModel->formatshortdatetime($publication['date']);
            $noticeListings .= $view->deathNotice($newspaper, $url, $datePublished);
        }
        return $noticeListings;
    }
    /**
     * Prepare the list of publications for the form
     *
     * The publications are received as a numerically indexed array. The keys are name and date
     * We need to convert this to a list of unique newspaper names.
     * The newspaper names also need to indicate how many times they appear, so we can use array_count_values
     *
     * @return array[int]string
     * @param array[string]string
     */
    function formPublications($publications)
    {
        $publicationNames = array();
        foreach ($publications as $publication) {
            $name = $this->getNewspaperName($publication['name']);
            array_push($publicationNames, $name);
        }
        $publicationNames = array_count_values($publicationNames);
        return $publicationNames;
    }
    function processErrors(&$get)
    {
        $errors = array(
            'publication' => !empty($get['publicationerror']),
            'email' => !empty($get['emailprovidederror']),
            'confirm' => !empty($get['emailmatcherror'])
        );
        return $errors;
    }
    function orderForm($personId, $fullName, $publications, $price, $errors)
    {
        $view = $this->_view;
        $formPublications = $this->formPublications($publications);
        $errorMessages = array();
        if ($errors['publication']) {
            $errorMessages['publication'] = '<p class="error">A publication needs be selected.</p>';
        }
        if ($errors['email']) {
            $errorMessages['email'] = '<p class="error">An email address needs to be provided.</p>';
        }
        if ($errors['confirm']) {
            $errorMessages['confirm'] = '<p class="error">The email addresses must match.</p>';
        }
        return $view->orderForm($personId, $fullName, $formPublications, $price, $errorMessages);
    }
    function noticeOrder($personId, $fullName, $publications, &$get) {
        $view = $this->_view;
        $price = 4.90;
        $errors = $this->processErrors($get);
        $order = $this->orderForm($personId, $fullName, $publications, $price, $errors);
        return $order;
    }
    function personNotices($personId, &$get)
    {
        $db = $this->_db;
        $view = $this->_view;
        $personModel = $this->_personModel;
        $text = '';
        $person = $db->getPerson($personId);
        if (empty($person)) {
            $text .= "<p>Sorry we have no matches for the names you supplied.</p>";
        } else {
            $personId = $person['id'];
            $publications = $db->getPublications($personId);
            $publishedNotices = count($publications);
            $personInfo = $this->personInfo($person, $publishedNotices);
            $noticeIntro = $view->noticeIntro();
            $noticeListings = $view->noticeListings($this->noticeListings($personId, $publications));
            $noticeSummary = $view->orderSummary($price);
            $fullName = $person['firstNames'] . ' ' . strtoupper($person['surname']);
            $noticeOrder = $this->noticeOrder($personId, $fullName, $publications, $get);
            return $view->noticePage($personInfo, $noticeIntro, $noticeListings, $noticeSummary, $noticeOrder);
        }
        return $text;
    }
}
?>